package com.bckj.fastboot.core.lang.model;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigInteger;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * PageResult
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PageResult<T> {

	/** 总记录数 */
	private BigInteger total;

	/** 列表数据 */
	private List<T> items;

	public <R> PageResult<R> convert(Function<T, R> mapper) {
		return PageResult.<R>builder()
				.total(total)
				.items(items != null && !items.isEmpty() ? items.stream().map(mapper).collect(Collectors.toList()) : Collections.emptyList())
				.build();
	}

	public static  <T> PageResult<T> of(IPage<T> page) {
		return PageResult.<T>builder()
				.total(BigInteger.valueOf(page.getPages()))
				.items(page.getRecords()).build();
	}
}
